//
//  BuyNowFloatingView.swift
//  iPhone
//
//  Created by Toj on 8/15/22.
//

import UIKit

private let cornerViewHeight: CGFloat = 64
private let buyButtonHeight: CGFloat = 44

class BuyNowFloatingView: UIView {
    
    var model: NSObject? {
        didSet {
           // guard let info = model else { return }
            
            // 折扣价
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineBreakMode = .byTruncatingTail
            paragraphStyle.alignment = .center
            let symbolAttributed = "₦".to_attributedString(font: UIFont.boldSystemFont(ofSize: 10), color: priceLabel.textColor, paragraph: paragraphStyle)
            let priceAttributed = "99999".to_attributedString(font: UIFont.boldSystemFont(ofSize: 21), color: priceLabel.textColor, paragraph: paragraphStyle)
            symbolAttributed.append(priceAttributed)
            priceLabel.attributedText = symbolAttributed
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
        
        var colors = ["#F5DB9D".to_color.cgColor, "#E8CD8C".to_color.cgColor]
        cornerView.gd_add(colors: colors, direction: .leftToRight)
        
        colors = ["#FFEEB5".to_color.cgColor, "#FFE498".to_color.cgColor]
        buyButton.gd_add(colors: colors, direction: .leftToRight)
    }
    
    private lazy var cornerView: UIImageView = {
        let view = UIImageView()
        view.cm_cornerRadius = cornerViewHeight * 0.5
        view.cm_border(color: "#E8CD8C".to_color)
        return view
    }()
    
    private lazy var priceLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor(hex:"#554422")
        view.font = UIFont.boldSystemFont(ofSize: 21)
        return view
    }()
  
    private lazy var buyButton: UIButton = {
        let view = UIButton()
        view.cm_cornerRadius = buyButtonHeight * 0.5
        view.setTitle("BUY NOW", for: .normal)
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        view.setTitleColor(UIColor(hex:"#775E33"), for: .normal)
        view.addTarget(self, action: #selector(didTapBuy), for: .touchUpInside)
        return view
    }()
    
    @objc private func didTapBuy() {
        
    }
  
    private lazy var agreeCheckBox: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setTitle("I agree with the", for: .normal)
        view.setTitleColor(UIColor(hex: "#666666"), for: .normal)
        view.setImage("commodity_agreeCheckBox_sel".to_image, for: .normal)
        view.setContentHuggingPriority(.dragThatCanResizeScene, for: .horizontal)
        view.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        view.sizeToFit()
        return view
    }()
    
    private lazy var serviceLabel: UILabel = {
        let view = UILabel()
        
        let font = UIFont.systemFont(ofSize: 14)
        let titleColor = UIColor(hex: "#0087EB")
        
        let attributed = "Terms of Service".to_attributedString(font: font, color: titleColor, lineStyle: .strikethrough)
        view.attributedText = attributed
        view.font = font
        view.textColor = titleColor
        
        return view
    }()
    
    deinit {
        // print("-->toj: CommodityHeaderView: deinit")
    }
}

// MARK: - 初始化
extension BuyNowFloatingView {
    
    private func setupMakeAddSubviews() {
            
        addSubview(cornerView)
        addSubview(priceLabel)
        addSubview(buyButton)
        addSubview(agreeCheckBox)
        addSubview(serviceLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        cornerView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(16)
            make.right.equalTo(snp.right).offset(-16)
            make.height.equalTo(cornerViewHeight)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(cornerView.snp.left).offset(24)
            make.centerY.equalTo(cornerView)
        }
        
        buyButton.snp.makeConstraints { make in
            make.right.equalTo(cornerView.snp.right).offset(-12)
            make.centerY.equalTo(cornerView)
            make.width.equalTo(104)
            make.height.equalTo(buyButtonHeight)
        }
        
        let width = agreeCheckBox.frame.width
        agreeCheckBox.snp.makeConstraints { make in
            make.left.equalTo(cornerView)
            make.height.equalTo(24)
            make.top.equalTo(cornerView.snp.bottom).offset(12)
            make.width.equalTo(width)
        }
        
        serviceLabel.snp.makeConstraints { make in
            make.left.equalTo(agreeCheckBox.snp.right).offset(2)
            make.centerY.equalTo(agreeCheckBox)
            make.right.equalTo(cornerView)
        }
    }
    
    private func setupRelayoutSubviews() {
        
    }
    
    private func setupMakeInitialize() {
        layoutIfNeeded()
        model = nil
    }
}
